package com.suns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suns.dao.UserDao;
import com.suns.domain.Admin;
import com.suns.domain.User;
import com.suns.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public User userlogin(String tel, String pwd) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", tel);
        User user =userDao.selectOne(wrapper);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return user != null && passwordEncoder.matches(pwd, user.getPwd()) ? user : null;
    }
    @Override
    public boolean sign(String tel, String pwd, String uType) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", tel);
        if (userDao.selectOne(wrapper) == null) {
            User user = new User();
            user.setTel(tel);
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String hashedPassword = passwordEncoder.encode(pwd);
            user.setPwd(hashedPassword);
            user.setUsertype(uType);
            user.setMoney(BigDecimal.valueOf(0));
            user.setCode1("无");
            user.setCode2("无");
            user.setCode3("无");
            return userDao.insert(user) > 0;
        } else {
            return false;
        }
    }
    @Override
    public User login(String tel, String pwd) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", tel);
        User user = userDao.selectOne(wrapper);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return user != null && passwordEncoder.matches(pwd, user.getPwd()) ? user : null;
    }

    @Override
    public User getBytel(String tel) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", tel);
        return userDao.selectOne(wrapper);
    }

    public User getById(Integer id) {
//        return bookDao.selectById(1);
        //多数据查找
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        return (User) userDao.selectBatchIds(list);
    }

    @Override
    public PageInfo<User> getPageUsers(int pageNum, int pageSize, String keywords) {
        // 使用 PageHelper 进行分页操作
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        if(!Objects.equals(keywords, "null")){
            String likeKeywords = "%" + keywords.toLowerCase() + "%";
            wrapper.like(User::getId,keywords)
                    .or().like(User::getName,likeKeywords)
                    .or().like(User::getUsertype,likeKeywords)
                    .or().like(User::getMoney,likeKeywords)
                    .or().like(User::getCode1,likeKeywords)
                    .or().like(User::getCode2,likeKeywords)
                    .or().like(User::getCode3,likeKeywords)
                    .or().like(User::getTel,likeKeywords);
        }
        List<User> userList = userDao.selectList(wrapper);
        return new PageInfo<>(userList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //开启事务
    public boolean delUsers(List<Integer> ids) {
        try {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
            wrapper.in(User::getId, ids);
            int rows=userDao.delete(wrapper);
            if(rows!=ids.size()){
                throw new RuntimeException("批量删除失败!"); // 抛出异常
            }
        } catch (Exception e) {
            throw new RuntimeException("批量删除失败!"); // 抛出异常
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //开启事务
    public boolean updateUser(String tel,String name, String pwd , String newpwd) {

        if (userlogin(tel, pwd) != null) {
            String hashedPassword = null;
            if (newpwd != null) {
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                hashedPassword = passwordEncoder.encode(newpwd);
            }
            LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<User>();
            wrapper.eq(User::getTel, tel)
                    .set(User::getName, name);
            if (hashedPassword != null) {
                wrapper.set(User::getPwd, hashedPassword);
            }
            int updateCount = userDao.update(null, wrapper);
            return updateCount > 0;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addlicense(String tel, String carnumber, String carnumber1, String carnumber2, String carnumber3){
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        if(!carnumber1.equals("无")){
             if(!carnumber2.equals("无")) {
                 wrapper.eq(User::getTel,tel)
                         .set(User::getCode3,carnumber);
                 return userDao.update(null, wrapper)==1;
                 }
             else {
                 wrapper.eq(User::getTel,tel)
                         .set(User::getCode2,carnumber);
                 return userDao.update(null, wrapper)==1;
             }
        }
        else{
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,carnumber);
             return userDao.update(null, wrapper)==1;
        }
    }
    public boolean modifylicense(String tel,String ocarnumber1, String ocarnumber2, String ocarnumber3, String carnumber1, String carnumber2, String carnumber3){
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        if(ocarnumber1.equals(carnumber1)&&ocarnumber2.equals(carnumber2)&&!ocarnumber3.equals(carnumber3))
        {
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode3,carnumber3);
        }
        if(ocarnumber1.equals(carnumber1)&&!ocarnumber2.equals(carnumber2)&&ocarnumber3.equals(carnumber3))
        {
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode2,carnumber2);
        }
        if(!ocarnumber1.equals(carnumber1)&&ocarnumber2.equals(carnumber2)&&ocarnumber3.equals(carnumber3))
        {
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,carnumber1);
        }
        if(!ocarnumber1.equals(carnumber1)&&!ocarnumber2.equals(carnumber2)&&ocarnumber3.equals(carnumber3))
        {

            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,carnumber1)
                     .set(User::getCode2,carnumber2);
        }
        if(!ocarnumber1.equals(carnumber1)&&ocarnumber2.equals(carnumber2)&&!ocarnumber3.equals(carnumber3))
        {

            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,carnumber1)
                    .set(User::getCode3,carnumber3);
        }
        if(ocarnumber1.equals(carnumber1)&&!ocarnumber2.equals(carnumber2)&&!ocarnumber3.equals(carnumber3))
        {

            wrapper.eq(User::getTel,tel)
                    .set(User::getCode2,carnumber2)
                    .set(User::getCode3,carnumber3);
        }
        if(!ocarnumber1.equals(carnumber1)&&!ocarnumber2.equals(carnumber2)&&!ocarnumber3.equals(carnumber3))
        {
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,carnumber1)
                    .set(User::getCode2,carnumber2)
            .set(User::getCode3,carnumber3);
        }
            return userDao.update(null,wrapper)==1;
    }
    public boolean deletelicense(String tel, String order1, String order2,String order3){
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        if(order1.equals("1")&&order2.equals("1")&&order3.equals("1")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,'无')
                    .set(User::getCode2,'无')
                    .set(User::getCode3,'无');
        }
        if(order1.equals(0)&&order2.equals("1")&&order3.equals("1")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode2,'无')
                    .set(User::getCode3,'无');
        }
        if(order1.equals("1")&&order2.equals("0")&&order3.equals("1")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,'无')
                    .set(User::getCode3,'无');
        }
        if(order1.equals("1")&&order2.equals("1")&&order3.equals("0")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,'无')
                    .set(User::getCode2,'无');
        }
        if(order1.equals("1")&&order2.equals("0")&&order3.equals("0")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode1,'无');
        }
        if(order1.equals("0")&&order2.equals("1")&&order3.equals("0")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode2,'无');
        }
        if(order1.equals("0")&&order2.equals("0")&&order3.equals("1")){
            wrapper.eq(User::getTel,tel)
                    .set(User::getCode3,'无');
        }
        return userDao.update(null,wrapper)==1;

    }


//    @Override
//    public List<User> getAll() {
//        UserQuery uq = new UserQuery(); //模拟查询数据
//        uq.setId(3);
//        uq.setId2(5);
////        方式三:lambda格式按条件查询
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();//添加限制条件
//        wrapper.gt(User::getId, 3).lt(User::getId, 5); //设多条件,查询id大于3并且小于5的所有数据
//        wrapper.gt(uq.getId2() != null, User::getId, uq.getId2())
//                .or()
//                .lt(uq.getId() != null, User::getId, uq.getId()); //设多条件,查询id大于5或者小于3的所有数据 (链式编程)
//        return userDao.selectList(wrapper);
//        //查询投影
////        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<User>();
////        wrapper.select(User::getId,User::getName);
////
////        QueryWrapper<User> wp=new QueryWrapper<User>();
////        wp.select("count(*) as count");
////        wp.groupBy("type");
////        List<Map<String,Object>> maps=bookDao.selectMaps(wp);
////        System.out.println(maps);
//    }

    //    @Override
//    public boolean update(User user) {
//        UserQuery uq=new UserQuery(); //模拟查询数据
//        uq.setId(2);
//        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<User>();//添加限制条件
//        wrapper.eq(uq.getId()!=null, User::getId,uq.getId());
//        return userDao.update(user,null) > 0;
//    }
}
